<?php

namespace App\Api\Controllers;

use App\Api\Models\SupplierCustomerModel;
use App\Api\Services\SupplierCustomerService;
use \App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class SupplierCustomerController extends BaseController
{
    /**
     * 新建供应商或客户
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/13 11:25
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'name' => 'required',//客户\供应商名称、
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

//        if(!empty($data['phone'])) {
//            $messages = [
//                'phone.required' => '手机号不能为空，请输入手机号',
//                'phone.regex' => '手机号格式错误，请重试',
//            ];
//
//            $validator1 = Validator::make($data, [
//                'phone' => 'regex:/^1[3456789][0-9]{9}$/',
//            ], $messages);
//
//            //参数错误响应
//            if ($info1 = $validator1->errors()->first()) {
//                return $this->fail('', $info1);
//            }
//        }
//        else {
//            $data['phone'] = '';
//        }

        $type = !isset($data['type'])?0:$data['type'];
        $data['compony_name'] = empty($data['compony_name'])?'':$data['compony_name'];
        $data['address'] = empty($data['address'])?'':$data['address'];

        $arr = [
            'compony_name' => $data['compony_name'],
            'name' => $data['name'],
            'phone' => isset($data['phone'])?$data['phone']:'',
            'address' => $data['address'],
            'home_phone' => isset($data['home_phone'])?$data['home_phone']:'',
            'remark' => isset($data['remark'])?$data['remark']:'',
            'main_id' => $this->getMainUserId(),
            'type' => $type,
            'user_id' => $this->getUserId(),
            'created_at' => time()
        ];

        $this->log('添加供应商和客户', $data);
        $id = SupplierCustomerModel::insertGetId($arr);
        return $this->success($id);
    }

    /**
     * 编辑供应商或客户信息
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 14:24
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//供应商客户表主键id
        ], [
            'id.required' => '参数`标识id`缺失',
            'id.integer' => '参数`标识id`类型错误'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

//        if(!empty($data['phone'])) {
//            $messages = [
//                'phone.required' => '手机号不能为空，请输入手机号',
//                'phone.regex' => '手机号格式错误，请重试',
//            ];
//
//            $validator1 = Validator::make($data, [
//                'phone' => 'regex:/^1[3456789][0-9]{9}$/',
//            ], $messages);
//
//            //参数错误响应
//            if ($info1 = $validator1->errors()->first()) {
//                return $this->fail('', $info1);
//            }
//        }

        $data['user_id'] = $this->getUserId();

        SupplierCustomerService::edit($data);

//        SupplierCustomerModel::where('id', $data['id'])->update($data);
        $this->log('编辑供应商或客户', $data);
        return $this->success();
    }

    /**
     * 删除供应商或客户
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 14:51
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//供应商客户表主键id
        ], [
            'id.required' => '参数`标识id`缺失',
            'id.integer' => '参数`标识id`类型错误'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        //操作日志
        $model = SupplierCustomerModel::find($data['id']);

        if (!empty($model)) {
            $model->delete();
        }

        $this->log('删除供应商或客户', $model);
        return $this->success();
    }

    /**
     * 列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 15:44
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' => 'required|integer',//页面大小
            'currentPage' => 'required|integer',//当前页
//            'type' => [
//                'required',
//                'integer',
//                Rule::In([1, 2, 0]),
//            ],//类型：0-供应商 1-客户 2-全部
        ], [
            'pageSize.required' => '参数`页面大小`缺失',
            'pageSize.integer' => '参数`页面大小`仅能为数字',
            'currentPage.required' => '参数`当前页码`缺失',
            'currentPage.integer' => '参数`当前页码`仅能为数字',
//            'type.required' => '参数`类型`缺失',
//            'type.integer' => '参数`类型`仅能为数字',
//            'type.in' => '参数`类型`不合法',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        //用户类型;0:主账号;1:子账号;
        $data['user_type'] = $this->getUserTpye();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();

        return $this->success(
            SupplierCustomerService::index(
                $data
            )
        );
    }

    /**
     * 供应商或客户详细
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 15:53
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//供应商客户表主键id
        ], [
            'id.required' => '参数`标识id`缺失',
            'id.integer' => '参数`标识id`类型错误'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(
            SupplierCustomerModel::select(
                'id', 'compony_name', 'name', 'phone', 'address', 'created_at'
            )
                ->find($data['id'])
        );
    }

    /**
     * 下拉
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 16:07
     * @return \Illuminate\Http\JsonResponse
     */
    public function select(Request $request)
    {
        return $this->success(
            SupplierCustomerModel::select('id','name')
            ->where(['main_id'=>$this->getMainUserId()])
            ->get()
        );
    }
}
